#include "b_knight.h"

#include "board.h"
#include "gameengine.h"
#include <iostream>

using namespace std;

Knight::Knight() {
}

Knight::Knight(int r, int c , Color color) {
	row = r;
	col = c;
	pColor = color;
	setImage();
}


Knight::~Knight() {

}

void Knight::setImage() {
	if(pColor==white) {
		image = W_KNIGHT;
	} else {
		image = B_KNIGHT;
	}
}

void Knight::GetAllPossible(std::vector <Cell>  & moves) {
	if(MyFunctions::isInRange(row+1,col+2)  && !(myBoard->hasTeamate(pColor,row+1,col+2)))
		moves.push_back(Cell(row+1,col+2));

	if(MyFunctions::isInRange(row-1,col+2)  && !(myBoard->hasTeamate(pColor,row-1,col+2)))
		moves.push_back(Cell(row-1,col+2));

	if(MyFunctions::isInRange(row-1,col-2)  && !(myBoard->hasTeamate(pColor,row-1,col-2)))
		moves.push_back(Cell(row-1,col-2));

	if(MyFunctions::isInRange(row+1,col-2)  && !(myBoard->hasTeamate(pColor,row+1,col-2)))
		moves.push_back(Cell(row+1,col-2));

	if(MyFunctions::isInRange(row+2,col+1)  && !(myBoard->hasTeamate(pColor,row+2,col+1)))
		moves.push_back(Cell(row+2,col+1));

	if(MyFunctions::isInRange(row-2,col+1)  && !(myBoard->hasTeamate(pColor,row-2,col+1)))
		moves.push_back(Cell(row-2,col+1));

	if(MyFunctions::isInRange(row-2,col-1)  && !(myBoard->hasTeamate(pColor,row-2,col-1)))
		moves.push_back(Cell(row-2,col-1));

	if(MyFunctions::isInRange(row+2,col-1)  && !(myBoard->hasTeamate(pColor,row+2,col-1)))
		moves.push_back(Cell(row+2,col-1));


}

bool Knight::isPiece() {
	return true;
}



void Knight::setCell(int r, int c) {
	row = r;
	col = c;
}

Color Knight::GetColor() {
	return pColor;
}

ImageName Knight::GetImage() {
	return image;
}

bool Knight::Test(std::ostream &os) {
	bool success = true;
	if (!Knight::TestMoves(std::cout)) success = false;
	if (!Knight::TestCheck(std::cout)) success = false;
	return success;
}

bool Knight::TestMoves(std::ostream &os) {

	bool success = true;

	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.myBoard.piecesOnBoard.push_back(new Knight(0,0,white));
	testGame.myBoard.piecesOnBoard.push_back(new Knight(0,7,white));
	testGame.myBoard.piecesOnBoard.push_back(new Knight(2,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Knight(5,6,white));

	testGame.myBoard.piecesOnBoard.push_back(new Queen(0,6,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(1,6,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(1,7,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(3,7,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(6,4,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(7,7,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(3,5,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(4,4,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(7,5,black));

	testGame.turn = white;
	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[1-1];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[2-1];
	ChessPiece * p3 = testGame.myBoard.piecesOnBoard[3-1];
	ChessPiece * p4 = testGame.myBoard.piecesOnBoard[4-1];

	p1->setBoard(&testGame.myBoard);
	p2->setBoard(&testGame.myBoard);
	p3->setBoard(&testGame.myBoard);
	p4->setBoard(&testGame.myBoard);

	std::vector<Cell> moves1;
	std::vector<Cell> moves2;
	std::vector<Cell> moves3;
	std::vector<Cell> moves4;

	p1->GetAllPossible(moves1);
	p2->GetAllPossible(moves2);
	p3->GetAllPossible(moves3);
	p4->GetAllPossible(moves4);

	std::vector<Cell>answer1;
	std::vector<Cell>answer2;
	std::vector<Cell>answer3;
	std::vector<Cell>answer4;
	/*

	0 1 2 3 4 5 6 7
	  0 k . . . . . q k TEST 1 | TEST 2
	  1 . . . . . . q q
	  2 . . k . . . . . TEST 3
	  3 . . . . . Q . q
	  4 . . . . Q . . .
	  5 . . . . . . k . TEST 4
	  6 . . . . q . . .
	  7 . . . . . Q . q
	  */

	answer1.push_back(Cell(2,1));
	answer1.push_back(Cell(1,2));

	answer2.push_back(Cell(1,5));
	answer2.push_back(Cell(2,6));

	answer3.push_back(Cell(1,0));
	answer3.push_back(Cell(3,0));
	answer3.push_back(Cell(4,1));
	answer3.push_back(Cell(4,3));
	answer3.push_back(Cell(3,4));
	answer3.push_back(Cell(1,4));
	answer3.push_back(Cell(0,1));
	answer3.push_back(Cell(0,3));

	answer4.push_back(Cell(3,5));
	answer4.push_back(Cell(4,4));
	answer4.push_back(Cell(7,5));


	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));
	TEST(Pawn::CheckCellVectors(moves3,answer3));
	TEST(Pawn::CheckCellVectors(moves4,answer4));


	return success;
}

bool Knight::TestCheck(std::ostream &os) {

	bool success = true;
	GameEngine testGame;
	testGame.gameStarted = true;
	testGame.turn = white;

	testGame.myBoard.piecesOnBoard.push_back(new Knight(5,2,white));
	testGame.myBoard.piecesOnBoard.push_back(new Knight(5,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new King(6,3,white));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(3,0,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(2,3,black));
	testGame.myBoard.piecesOnBoard.push_back(new Queen(4,4,black));


	ChessPiece * p1 = testGame.myBoard.piecesOnBoard[0];
	ChessPiece * p2 = testGame.myBoard.piecesOnBoard[1];


	std::vector<ChessPiece *>::iterator it;
	for ( it = testGame.myBoard.piecesOnBoard.begin() ;
	        it < testGame.myBoard.piecesOnBoard.end() ; it++) {
		ChessPiece * p = *it;
		p->setBoard(&testGame.myBoard);
	}

	std::vector<Cell> moves1;
	std::vector<Cell> moves2;

	testGame.FindMoves(moves1,p1);
	testGame.FindMoves(moves2,p2);

	std::vector<Cell> answer1;
	std::vector<Cell> answer2;


	TEST(Pawn::CheckCellVectors(moves1,answer1));
	TEST(Pawn::CheckCellVectors(moves2,answer2));

	return success;
}

int Knight::getValue()
{
    return 3;
}


/*
  TEST MOVES------------------------

  Capital = black
  lower = white
  p = pawn
  q = Queen
  k = Knight

    0 1 2 3 4 5 6 7
  0 k . . . . . p k TEST 1 | TEST 2
  1 . . . . . . p p
  2 . . k . . . . . TEST 3
  3 . . . . . Q . q
  4 . . . . Q . . .
  5 . . . . . . k . TEST 4
  6 . . . . q . . .
  7 . . . . . Q . q

  TEST CHECK------------------------

  Capital = black
  lower = white
  n = Knight
  q = queen
  k = king

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . Q . . . .
  3 Q . . . . . . .
  4 . . . . Q . . .
  5 . . n n . . . . TEST 1 | TEST 2
  6 . . . k . . . .
  7 . . . . . . . .

    0 1 2 3 4 5 6 7
  0 . . . . . . . .
  1 . . . . . . . .
  2 . . . . . . . .
  3 . . . . . . . .
  4 . . . . . . . .
  5 . . . . . . . .
  6 . . . . . . . .
  7 . . . . . . . .


  */
